package fiuba.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityExistsException;

import org.springframework.dao.DataIntegrityViolationException;

import fiuba.dao.MenuItemDao;
import fiuba.service.MenuExistsException;
import fiuba.service.MenuItemManager;
import fiuba.model.LabelValue;
import fiuba.model.MenuItem;

public class MenuItemManagerImpl extends UniversalManagerImpl implements
	MenuItemManager {
private MenuItemDao dao;

public void setMenuItemDao(MenuItemDao dao){
	this.dao = dao;
}

public MenuItem getMenuItem(String menuItemId){
	return dao.get(new Long(menuItemId));
}


public List<MenuItem> getMenuItems() {
	return dao.getMenuItems();
}



public void removeMenuItem(String menuItemId) {
	log.debug("removing MenuItem with id:" + menuItemId);
	dao.remove(new Long(menuItemId));
}

public MenuItem saveMenuItem(MenuItem menuItem) throws MenuExistsException {
//	menuItem.setName(menuItem.getName().toLowerCase());
    
    try {
        return dao.saveMenuItem(menuItem);
    } catch (DataIntegrityViolationException e) {
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new MenuExistsException("Menu '" + menuItem.getName() + "' already exists!");
    } catch (EntityExistsException e) { // needed for JPA
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new MenuExistsException("Menu '" + menuItem.getName() + "' already exists!");
    }
}


}
